package servlet;

import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet("/UserOptionsServlet")
    public class UserOptionsServlet extends HttpServlet {
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 在这里处理用户选项的逻辑，这里简单示例返回一个固定的选项列表
            List<Map<String, String>> options = new ArrayList<>();
            Map<String, String> option1 = new HashMap<>();
            option1.put("name", "个人资料");
            option1.put("url", "user/profile.jsp");
            options.add(option1);

            Map<String, String> option2 = new HashMap<>();
            option2.put("name", "设置");
            option2.put("url", "user/settings.jsp");
            options.add(option2);

            Map<String, String> option3 = new HashMap<>();
            option3.put("name", "退出");
            option3.put("url", "user/logout.jsp");
            options.add(option3);

            // 将选项列表以 JSON 格式返回给客户端
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            out.print(new Gson().toJson(options));
            out.flush();
        }
    }


